package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.LanguageService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.Where;
import com.ccys.db.dto.masterData.LanguageDelDto;
import com.ccys.db.dto.masterData.LanguagePageDto;
import com.ccys.db.dto.masterData.LanguageSaveDto;
import com.ccys.db.entity.TblLanguage;
import com.ccys.db.mapper.TblLanguageMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.LanguageListVo;
import com.ccys.db.vo.masterData.LanguagePageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 语言实现类
 * @author: ZhangJingCan
 * @create: 2023-12-11 09:11
 **/

@Service
public class LanguageServiceImpl extends BaseServiceImpl<TblLanguage> implements LanguageService {

    @Resource
    private TblLanguageMapper tblLanguageMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(LanguageSaveDto dto) {
        if (ObjectUtil.isNotEmpty(dto.getName())) {
            Where where = new Where();
            where.addWhere("name = '" + dto.getName() + "'");
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                where.addWhere("id != '" + dto.getId() + "'");
            }
            List<TblLanguage> list = this.findListByLimit(TblLanguage.class, where.getWhereList(), null);
            if (!list.isEmpty()) {
                return ResponseResult.error("Name exists！");
            }
        }
        if (StringUtil.isEmpty(dto.getId())) {
            dto.setStatus(1);
        }
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteLanguageById(LanguageDelDto dto) {
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error！");
        }
        TblLanguage tblLanguage = new TblLanguage();
        tblLanguage.setId(dto.getId());
        tblLanguage.setDelFlag(1);
        if (this.updateEntityById(tblLanguage) > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.error("Delete Failed！");
    }

    @Override
    public ResponseResult findPage(LanguagePageDto dto) {
        //判断如果值不在范围则赋值为null
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        PageDto.startPage();
        List<LanguagePageQueryVo> list = tblLanguageMapper.findPage(dto);
        PageInfo<LanguagePageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateStatus(LanguageSaveDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblLanguage tblLanguage = new TblLanguage();
        tblLanguage.setId(dto.getId());
        tblLanguage.setStatus(dto.getStatus());
        if (tblLanguageMapper.updateByPrimaryKeySelective(tblLanguage) > 0) {
            //保存修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<List<LanguageListVo>, Object> findList() {
        List<LanguageListVo> list = tblLanguageMapper.findList();
        return ResponseResult.success(list);
    }
}
